Skip to content

History Reduction

HistoryReductionConfig controls how the agent manages long conversation histories. When enabled, the agent trims or summarizes old messages before each turn to keep the context window under control.

csharp
var config = new AgentConfig
{
    HistoryReduction = new HistoryReductionConfig
    {
        Enabled = true,
        Strategy = HistoryReductionStrategy.MessageCounting,
        TargetCount = 20
    }
};

Properties

Core

PropertyTypeDefaultDescription
EnabledboolfalseEnable history reduction
StrategyHistoryReductionStrategyMessageCountingHow history is reduced — count-based or AI summarization
BehaviorHistoryReductionBehaviorContinueWhat happens when reduction fires — transparent or circuit-breaker
CountingUnitHistoryCountingUnitExchangesUnit of measurement for TargetCount
TargetCountint20Number of units to retain after reduction
SummarizationThresholdint?5Units beyond TargetCount before summarization fires (Summarizing strategy only)

Summarization

PropertyTypeDefaultDescription
CustomSummarizationPromptstring?nullOverride the default summarization prompt
SummarizerProviderProviderConfig?nullUse a separate (cheaper) provider for summarization
UseSingleSummarybooltrueRe-summarize all history each time (true) vs. append incremental summaries (false)

Token-Budget Properties (Not Yet Implemented)

These properties exist on HistoryReductionConfig but are not currently evaluated by the framework. They are reserved for a future token-aware reduction mode.

PropertyTypeDefault
TargetTokenBudgetint4000
TokenBudgetThresholdint1000
TokenBudgetTriggerPercentagedouble?null
TokenBudgetPreservePercentagedouble0.3
ContextWindowSizeint?null

Do not rely on these properties to have any effect.


Enums

HistoryReductionStrategy

ValueDescription
MessageCountingDrop oldest messages until TargetCount units remain
SummarizingSummarize old messages into a compact context block using an LLM call

HistoryReductionBehavior

ValueDescription
ContinueReduction happens silently mid-turn — the agent keeps going without interruption
CircuitBreakerStop the current turn and notify the user that history was reduced before continuing

HistoryCountingUnit

ValueDescription
ExchangesCount user↔agent pairs (one user message + one agent reply = 1 exchange)
MessagesCount raw ChatMessage objects (including tool calls and results)

Examples

MessageCounting (simple)

Keep the last 10 user↔agent exchanges, drop the rest:

csharp
HistoryReduction = new HistoryReductionConfig
{
    Enabled = true,
    Strategy = HistoryReductionStrategy.MessageCounting,
    CountingUnit = HistoryCountingUnit.Exchanges,
    TargetCount = 10
}

Summarizing (richer context)

Summarize old history instead of dropping it, using a cheaper model:

csharp
HistoryReduction = new HistoryReductionConfig
{
    Enabled = true,
    Strategy = HistoryReductionStrategy.Summarizing,
    TargetCount = 15,
    SummarizationThreshold = 5,       // Summarize when 20+ exchanges exist
    SummarizerProvider = new ProviderConfig
    {
        ProviderKey = "openai",
        ModelName = "gpt-4o-mini"     // Cost-optimized summarizer
    },
    CustomSummarizationPrompt = "Summarize this conversation concisely, preserving key facts and decisions."
}

Circuit-breaker behavior

Stop the turn and inform the user before compressing history:

csharp
HistoryReduction = new HistoryReductionConfig
{
    Enabled = true,
    Strategy = HistoryReductionStrategy.MessageCounting,
    Behavior = HistoryReductionBehavior.CircuitBreaker,
    TargetCount = 20
}

Per-Run Overrides

You can override reduction behavior for a single turn via AgentRunConfig:

csharp
// Force reduction now (before an expensive operation)
var options = new AgentRunConfig { TriggerHistoryReduction = true };

// Skip reduction (need full context for an important decision)
var options = new AgentRunConfig { SkipHistoryReduction = true };

// Change behavior mode for this turn
var options = new AgentRunConfig
{
    HistoryReductionBehaviorOverride = HistoryReductionBehavior.CircuitBreaker
};

JSON Example

json
{
    "HistoryReduction": {
        "Enabled": true,
        "Strategy": "MessageCounting",
        "Behavior": "Continue",
        "CountingUnit": "Exchanges",
        "TargetCount": 20,
        "SummarizationThreshold": 5,
        "UseSingleSummary": true,
        "CustomSummarizationPrompt": null,
        "SummarizerProvider": null
    }
}

See Also

Released under the MIT License.